package com.shujia.mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Partitioner;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

import java.io.IOException;

public class Demo4Partition {

    static class PartitionMap extends Mapper<LongWritable, Text, Text, LongWritable> {
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

            String[] split = value.toString().split(",");
            String s_id = split[0];
            int score = Integer.parseInt(split[2]);

            context.write(new Text(s_id), new LongWritable(score));

        }
    }

    static class PartitionReduce extends Reducer<Text, LongWritable, NullWritable, Text> {
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {

            long sumSco = 0;
            //计算总分
            for (LongWritable value : values) {

                sumSco = sumSco + value.get();
            }

            String s_id = key.toString();
            String line = s_id + "," + sumSco;

            context.write(NullWritable.get(), new Text(line));

        }
    }


    static class MyPartitioner extends Partitioner<Text, LongWritable> {

        /**
         * 每一条数据调用一次
         * <p>
         * 保证同一个key能获取到相同的分区就行
         */
        @Override
        public int getPartition(Text text, LongWritable longWritable, int numPartitions) {

            /**
             * 1500100004
             * 1500100005
             */

            String s_id = text.toString();

            int partition = Integer.parseInt(s_id.substring(s_id.length() - 1));


            return partition;

        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        /**
         *
         * 构建job任务
         *
         */

        Configuration conf = new Configuration();

        Job job = Job.getInstance(conf);

        job.setNumReduceTasks(10);

        //设置自定义分区器
        job.setPartitionerClass(MyPartitioner.class);

        job.setJobName("Demo4Partition");
        job.setJarByClass(Demo4Partition.class);

        job.setMapperClass(PartitionMap.class);

        job.setReducerClass(PartitionReduce.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setOutputKeyClass(NullWritable.class);

        job.setOutputValueClass(Text.class);

        TextInputFormat.addInputPath(job, new Path("/data/score"));

        TextOutputFormat.setOutputPath(job, new Path("/data/out_partition"));


        job.waitForCompletion(true);


    }
}
